using System.Collections;
using Photon;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerGravyGun : Photon.MonoBehaviour
{
	private Camera m_Camera;

	private bool m_Get;

	private bool m_LeftCtrl;

	private GameObject m_objectGet;

	private Rigidbody m_objectRig;

	private ItemFunctions m_objectItemFunction;

	private float m_LookAngle;

	private float m_distObject;

	public Image m_CanvasDelObj;

	private PlayerSetup m_PlayerSetup;

	public Transform PointTransform;

	private Rigidbody PointTransformRig;

	private float m_MinLaunchForce = 15f;

	private float m_MaxLaunchForce = 30f;

	private float m_MaxChargeTime = 0.75f;

	private float m_CurrentLaunchForce;

	private float m_ChargeSpeed;

	private bool m_Fired;

	private bool GetButton;

	[HideInInspector]
	public bool m_delObject;

	public Transform gunEnd;

	private LineRenderer laserLine;

	private bool m_ScrolMinus;

	private bool m_ScrolPlus;

	private void Start()
	{
		m_PlayerSetup = GetComponent<PlayerSetup>();
		if (base.photonView.isMine || SavesAllFunctions.Local)
		{
			m_Camera = m_PlayerSetup.s_RigidibodyFPSController.cam;
			PointTransform = new GameObject().transform;
			PointTransform.name = "BuldingTranform";
			PointTransform.gameObject.AddComponent<Rigidbody>();
			PointTransformRig = PointTransform.gameObject.GetComponent<Rigidbody>();
			PointTransformRig.useGravity = false;
			m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
			laserLine = gunEnd.GetComponent<LineRenderer>();
		}
	}

	private void Update()
	{
		if (PhotonNetwork.room == null || PhotonNetwork.offlineMode || base.photonView.isMine)
		{
			if (Input.GetKeyDown(KeyCode.L))
			{
				Cursor.lockState = CursorLockMode.Locked;
				Cursor.visible = false;
			}
			if (Input.GetKeyDown(KeyCode.J))
			{
				Cursor.lockState = CursorLockMode.None;
				Cursor.visible = true;
			}
		}
	}

	public void UpdateGravyGun()
	{
		LauchForceUpdate();
		if (Input.GetKey(KeyCode.Q))
		{
			DeleteObject();
		}
		if (Input.GetKeyUp(KeyCode.Q))
		{
			StopDeleteObject();
		}
		if (m_LeftCtrl && (bool)m_objectGet)
		{
			RotateObject();
		}
		if (m_objectGet != null && m_Get && m_objectGet.tag == "Item" && !m_LeftCtrl)
		{
			float axis = CrossPlatformInputManager.GetAxis("Mouse X");
			m_LookAngle += axis * m_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity;
			PointTransform.rotation = Quaternion.Slerp(PointTransform.rotation, Quaternion.Euler(PointTransform.eulerAngles.x, m_LookAngle, PointTransform.eulerAngles.z), 10f * Time.deltaTime);
		}
		if (m_objectGet != null && m_Get)
		{
			laserLine.SetPosition(0, gunEnd.position);
			laserLine.SetPosition(1, PointTransform.position);
			m_distObject += Input.mouseScrollDelta.y;
			m_distObject = Mathf.Clamp(m_distObject, 1f, 100f);
		}
		if (Input.GetKeyDown(KeyCode.F) && m_Get && m_objectGet != null)
		{
			DropObject(true);
			m_objectItemFunction.SetObjectKinematick(true);
		}
		if (m_ScrolPlus && m_Get && m_objectGet != null)
		{
			m_distObject += 10f * Time.deltaTime;
			m_distObject = Mathf.Clamp(m_distObject, 1f, 100f);
		}
		if (m_ScrolMinus && m_Get && m_objectGet != null)
		{
			m_distObject -= 10f * Time.deltaTime;
			m_distObject = Mathf.Clamp(m_distObject, 1f, 100f);
		}
	}

	private void RotateObject()
	{
		m_objectRig.angularVelocity = Vector3.zero;
		Vector3 normalized = PointTransform.transform.InverseTransformDirection(m_Camera.transform.TransformDirection(Vector3.right)).normalized;
		PointTransform.transform.Rotate(normalized, CrossPlatformInputManager.GetAxisRaw("Mouse Y"));
		Vector3 normalized2 = PointTransform.transform.InverseTransformDirection(m_Camera.transform.TransformDirection(Vector3.up)).normalized;
		PointTransform.transform.Rotate(normalized2, 0f - CrossPlatformInputManager.GetAxisRaw("Mouse X"));
	}

	public void LeftCtrl()
	{
		if (PhotonNetwork.room == null || PhotonNetwork.offlineMode || base.photonView.isMine)
		{
			m_LeftCtrl = false;
			if ((bool)m_objectGet)
			{
				m_LookAngle = PointTransform.transform.eulerAngles.y;
			}
			m_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = m_PlayerSetup.currentSensivity;
			m_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = m_PlayerSetup.currentSensivity;
		}
	}

	private void FixedUpdate()
	{
		if ((PhotonNetwork.room == null || PhotonNetwork.offlineMode || base.photonView.isMine) && m_objectGet != null && m_Get)
		{
			Vector3 vector = m_Camera.transform.position + m_Camera.transform.forward * m_distObject;
			Vector3 vector2 = vector - PointTransform.position;
			Vector3 normalized = vector2.normalized;
			float magnitude = vector2.magnitude;
			magnitude = magnitude * 2f + Mathf.Pow(magnitude, 2f) * (float)magnitude.CompareTo(0f);
			vector2 = normalized * magnitude;
			float num = Vector3.Distance(m_Camera.transform.position, PointTransform.position);
			Vector3 normalized2 = (PointTransform.position - m_Camera.transform.position).normalized;
			float num2 = m_distObject - num;
			num2 = num2 * 2f + Mathf.Pow(num2, 2f) * (float)magnitude.CompareTo(0f);
			Vector3 vector3 = normalized2 * num2;
			vector2 += vector3;
			PointTransformRig.velocity = vector2;
		}
	}

	private IEnumerator IEGetObject(ItemFunctions itemFunctions)
	{
		yield return new WaitForSeconds(0.125f);
		itemFunctions.GetObject(base.gameObject);
		itemFunctions.SetObjectKinematick(true);
	}

	public void GetObject()
	{
		if (!m_Get && !m_PlayerSetup.s_PlayerHealth.m_Dead)
		{
			Ray ray = new Ray(m_Camera.transform.position, m_Camera.transform.forward);
			Vector3 vector = m_Camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
			StartCoroutine(ShotEffect());
			laserLine.SetPosition(0, gunEnd.position);
			RaycastHit hitInfo;
			if (Physics.Raycast(vector, m_Camera.transform.forward, out hitInfo, 30f))
			{
				laserLine.SetPosition(1, hitInfo.point);
				if (!(hitInfo.transform.tag == "Item") && !(hitInfo.transform.tag == "Car"))
				{
					return;
				}
				ItemFunctions component = hitInfo.transform.gameObject.GetComponent<ItemFunctions>();
				if (GetObjectOrNo(hitInfo.transform.gameObject, component))
				{
					PointTransform.position = hitInfo.point;
					PointTransform.rotation = base.transform.rotation;
					m_objectGet = hitInfo.transform.gameObject;
					m_Get = true;
					m_distObject = Vector3.Distance(m_Camera.transform.position, hitInfo.point);
					m_objectRig = hitInfo.transform.GetComponent<Rigidbody>();
					m_objectRig.isKinematic = true;
					m_objectRig.transform.parent = PointTransform.transform;
					m_LookAngle = PointTransform.transform.eulerAngles.y;
					m_objectItemFunction = component;
					if (hitInfo.transform.tag == "Item")
					{
						m_objectRig.freezeRotation = true;
					}
					if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode && !component.photonView.isMine)
					{
						component.photonView.RequestOwnership();
						StopCoroutine("IEGetObject");
						StartCoroutine("IEGetObject", component);
					}
					else
					{
						component.GetObject(base.gameObject);
						component.SetObjectKinematick(true);
					}
				}
			}
			else
			{
				laserLine.SetPosition(1, vector + m_Camera.transform.forward * 30f);
			}
		}
		else if (m_Get)
		{
			DropObject(false);
		}
	}

	private IEnumerator ShotEffect()
	{
		laserLine.enabled = true;
		yield return new WaitForSeconds(0.1f);
		if (!m_Get)
		{
			laserLine.enabled = false;
		}
	}

	private bool GetObjectOrNo(GameObject obj, ItemFunctions itemFunct)
	{
		bool result = false;
		if (obj.tag == "Item" && !itemFunct.m_Take)
		{
			result = true;
		}
		if (obj.tag == "Car" && !itemFunct.s_CarFunct.m_PlayerInCar)
		{
			result = true;
		}
		return result;
	}

	public void DropObject(bool isKinematick)
	{
		if (m_Get && m_objectGet != null)
		{
			m_Get = false;
			m_objectRig.freezeRotation = false;
			m_objectRig.constraints = RigidbodyConstraints.None;
			if (!isKinematick)
			{
				m_objectItemFunction.SetObjectKinematick(false);
			}
			m_objectItemFunction.DropObject();
			m_objectRig.transform.parent = null;
			m_objectRig.isKinematic = false;
			m_objectRig.useGravity = true;
			m_objectGet = null;
			m_objectRig = null;
			m_LeftCtrl = false;
			laserLine.enabled = false;
			PointTransform.rotation = Quaternion.identity;
			if (!m_Fired)
			{
				GetButton = false;
				m_CurrentLaunchForce = m_MinLaunchForce;
				m_Fired = true;
			}
		}
		else if (m_Get && m_objectGet == null)
		{
			if (m_objectRig != null)
			{
				m_objectRig.transform.parent = null;
				m_objectRig.isKinematic = false;
				m_objectRig.useGravity = true;
			}
			m_Get = false;
			m_objectGet = null;
			m_objectRig = null;
			m_LeftCtrl = false;
			if (!m_Fired)
			{
				GetButton = false;
				m_CurrentLaunchForce = m_MinLaunchForce;
				m_Fired = true;
			}
		}
		LeftCtrl();
	}

	private void LauchForceUpdate()
	{
		if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
		{
			m_CurrentLaunchForce = m_MaxLaunchForce;
			ForceObjcet();
		}
		else if (GetButton && !m_Fired)
		{
			m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
		}
	}

	public void GetButtonDownFire()
	{
		if (m_Get)
		{
			m_Fired = false;
			GetButton = true;
			m_CurrentLaunchForce = m_MinLaunchForce;
		}
	}

	public void GetButtonUpFire()
	{
		if (m_Get)
		{
			ForceObjcet();
		}
	}

	public void ForceObjcet()
	{
		if (!m_Fired)
		{
			GetButton = false;
			if (m_Get)
			{
				m_objectRig.transform.parent = null;
				m_objectRig.useGravity = true;
				m_Fired = true;
				m_Get = false;
				m_objectRig.isKinematic = false;
				m_objectRig.freezeRotation = false;
				m_objectRig.velocity = m_Camera.transform.forward * m_CurrentLaunchForce * 1.5f;
				m_objectItemFunction.DropObject();
				m_objectItemFunction.SetObjectKinematick(false);
				m_objectGet = null;
				m_objectRig = null;
				m_LeftCtrl = false;
				laserLine.enabled = false;
				m_CurrentLaunchForce = m_MinLaunchForce;
				m_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = m_PlayerSetup.currentSensivity;
				m_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = m_PlayerSetup.currentSensivity;
			}
		}
	}

	public void DeleteObject()
	{
		if (m_PlayerSetup.s_PlayerHealth.m_Dead)
		{
			return;
		}
		Ray ray = new Ray(m_Camera.transform.position, m_Camera.transform.forward);
		RaycastHit hitInfo;
		if ((!Physics.Raycast(ray, out hitInfo, 20f) && !Physics.Raycast(ray, out hitInfo, 20f)) || (!(hitInfo.transform.tag == "Item") && !(hitInfo.transform.tag == "Car") && !(hitInfo.transform.tag == "Helicopter") && !(hitInfo.transform.tag == "Bike") && !(hitInfo.transform.tag == "Tank")))
		{
			return;
		}
		ItemFunctions component = hitInfo.transform.gameObject.GetComponent<ItemFunctions>();
		if (GetObjectOrNo(hitInfo.transform.gameObject, component))
		{
			if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode && !component.photonView.isMine)
			{
				component.photonView.RequestOwnership();
			}
			GameObject value = hitInfo.transform.gameObject;
			m_delObject = true;
			m_Get = true;
			StartCoroutine("DeleteIE", value);
		}
	}

	private IEnumerator DeleteIE(GameObject delObject)
	{
		int i = 0;
		while (delObject != null && m_delObject)
		{
			yield return new WaitForSeconds(0.25f);
			if (!m_delObject)
			{
				break;
			}
			Ray rayDel = new Ray(m_Camera.transform.position, m_Camera.transform.forward);
			RaycastHit hitDel;
			if (Physics.Raycast(rayDel, out hitDel, 20f) || Physics.Raycast(rayDel, out hitDel, 20f))
			{
				if (hitDel.transform.gameObject == delObject && (hitDel.transform.tag == "Item" || hitDel.transform.tag == "Car" || hitDel.transform.tag == "Helicopter" || hitDel.transform.tag == "Bike" || hitDel.transform.tag == "Tank") && !m_PlayerSetup.s_PlayerHealth.m_Dead)
				{
					i++;
					if (i == 3)
					{
						if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
						{
							PhotonNetwork.Destroy(hitDel.transform.gameObject);
						}
						else
						{
							Object.Destroy(hitDel.transform.gameObject);
						}
						delObject = null;
						m_delObject = false;
						m_Get = false;
						m_objectItemFunction = null;
					}
				}
				else
				{
					delObject = null;
					m_delObject = false;
					m_Get = false;
					m_objectItemFunction = null;
				}
			}
			else
			{
				m_Get = false;
				m_delObject = false;
				delObject = null;
				m_objectItemFunction = null;
			}
		}
	}

	public void StopDeleteObject()
	{
		if (m_delObject)
		{
			StopCoroutine("DeleteIE");
			m_delObject = false;
			m_Get = false;
		}
	}

	public void ScrollObjectMinusUp()
	{
		m_ScrolMinus = false;
	}

	public void ScrollObectMinusDown()
	{
		m_ScrolMinus = true;
	}

	public void ScrollObjectPlusUp()
	{
		m_ScrolPlus = false;
	}

	public void ScrollObectPlusDown()
	{
		m_ScrolPlus = true;
	}

	public void RotateObjectDown()
	{
		if (!m_LeftCtrl)
		{
			if (m_Get && m_objectGet != null)
			{
				m_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = 0f;
				m_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = 0f;
				m_LeftCtrl = true;
			}
		}
		else
		{
			LeftCtrl();
		}
	}

	public void KinematickObject()
	{
		if (m_Get && m_objectGet != null)
		{
			ItemFunctions component = m_objectGet.GetComponent<ItemFunctions>();
			DropObject(true);
			component.SetObjectKinematick(true);
		}
	}
}
